package info.openlvb.openlvg.client;

import info.openlvb.openlvg.client.handler.Login_Ok_Handler;
import info.openlvb.openlvg.client.images.OpenPageImages;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.gwtext.client.util.CSS;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Openlvg implements EntryPoint {
	RootPanel rootPanel;
	FormPanel formP;
	TextBox loginbox;
	PasswordTextBox passwordbox;
	Button okbutton;
	
	OpenPageImages images = GWT.create(OpenPageImages.class);

	public void onModuleLoad() {
		 
		rootPanel = RootPanel.get();
		formP = new FormPanel();
		rootPanel.addStyleName("rootPanel");
		rootPanel.setSize("100%", "100%");
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setSpacing(8);
		FlowPanel fp = new FlowPanel();
		fp.add(new HTML(images.openlvg().getHTML()));
		fp.addStyleName("LoginImage");
		vPanel.add(fp);
		vPanel.add(createAdvancedForm());
		vPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		vPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		formP.add(vPanel);
		formP.addStyleName("LoginPanel");
		CSS.swapStyleSheet("theme", "openlvg/ext/xtheme-gray.css");

		Timer timer = new Timer() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Openlvg.this.rootPanel.add(formP);
				RootPanel.getBodyElement().removeChild(RootPanel.get("loadingWrapper").getElement());
				loginbox.setFocus(true);
			}
		}; 
		timer.schedule(2000);
	}   
	private Widget createAdvancedForm() {
		// Create a table to layout the form options
		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		layout.setWidth("300px");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		layout.setHTML(0, 0, "<h4>Welcome on OpenPage for OpenLVG</h4>");
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		// Add some standard form options
		layout.setHTML(1, 0, "<h5>Login : </h5>");
		loginbox = new TextBox();
		layout.setWidget(1, 1, loginbox);
		passwordbox = new PasswordTextBox();
		layout.setHTML(2, 0, "<h5>Password</h5>");
		layout.setWidget(2, 1, passwordbox);
		

		HorizontalPanel HPanel=new HorizontalPanel();
		okbutton = new Button("Ok");
		HPanel.add(okbutton);
		Button resetbutton = new Button("Reset");
		HPanel.add(resetbutton);
		layout.setWidget(3, 1, HPanel);
		
		Login_Ok_Handler handler = new Login_Ok_Handler(this);//rootPanel,loginbox.getText(), passwordbox .getText());
		okbutton.addClickHandler(handler);
		passwordbox.addKeyUpHandler(handler);

		// Wrap the contents in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(layout);

		return decPanel;
	}
	public RootPanel getRootPanel(){
		return rootPanel;
	}
	public String getLogin(){
		return loginbox.getText();
	}
	public String getPassword(){
		return passwordbox.getText();
	}
}
